<template>
  <div class='container'>
    <!-- 新增编辑的弹层 -->
      <el-dialog :visible="showDialog" @close="closeDialog"  :title="zhan" :close-on-click-modal="false" width="450px">
        <!-- 表单内容 -->
  <el-form ref="addForm" label-width="120px" :model="formData" :rules="rules">
 <el-form-item label="所属学科" prop="subjectID">
        <el-select
          style="width: 80%"
          placeholder="请选择"
          v-model=" formData.subjectID"
          @focus="xiala"
        >
         <el-option
            v-for="item in items"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
 <el-form-item label="目录名称" prop="tagName"
 >
        <el-input
          style="width: 80%"
          v-model="formData.tagName"
          placeholder="请输入目录名称"
        />
      </el-form-item>
     </el-form>

        <template #footer>
          <div style="text-align: right;">
            <el-button @click="closeDialog">取消</el-button>
            <el-button type="primary" @click="queadd">确定</el-button>
          </div>
        </template>
      </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/tags.js'
import { simple as simpless } from '@/api/hmmm/subjects'
export default {
  data () {
    return {
      formData: {
        // 所属学科
        subjectID: '',
        // 标签
        tagName: ''
      },
      adddata: {
        // 所属学科
        subjectID: '',
        // 目录名
        tagName: ''
      },

      rules: {
        // 学科验证校验
        subjectID: [
          { required: true, message: '请选择所属学科', trigger: 'blur' }
        ],

        // 目录验证
        tagName: [
          { required: true, message: '请输入标签名称', trigger: 'blur' }
        ]
      },
      items: []
    }
  },
  computed: {
    zhan () {
      return this.formData.id ? '修改目录' : '新增目录'
    }
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    // x
    closeDialog () {
      this.$emit('update:showDialog', false)
    },
    /**
     * 新增
     */
    queadd () {
      this.$refs.addForm.validate(async flag => {
        if (!flag) return
        if (this.formData.id) {
          await update(this.formData)
          this.$message.success('修改成功')
        } else {
          await add(this.formData)
          this.$message.success('添加成功')
          this.$refs.addForm.resetFields()
        }
      })
      this.closeDialog()
      // zinf+.*
      this.$parent.getList()
    },
    async xiala () {
      const { data } = await simpless()
      console.log(data)
      this.items = data
    },
    // 修改回显
    async huixian (row) {
      this.xiala()
      const { data } = await detail(row)
      this.formData = data
      // console.log(this.formData)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
